﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Wpf.CommonTools.StringExtension
{
    public static class StringExtension
    {
        /// <summary>
        ///  限制长度(按照中文计算)
        /// </summary>
        /// <param name="input"></param>
        /// <param name="length"></param>
        /// <param name="lastStr"></param>
        /// <returns></returns>
        public static string ToLimitLength(this string input, int length, string lastStr = "...")
        {
            string value = input.ToLimitLength(length, 0);
            if (!string.IsNullOrEmpty(value) && value.Length < input.Length)
            {
                value += lastStr;
            }
            return value;
        }
        /// <summary>
        ///  限制长度(按照中文计算)
        /// </summary>
        /// <param name="input"></param>
        /// <param name="length"></param>
        /// <param name="length1"></param>
        /// <returns></returns>
        public static string ToLimitLength(this string input, int length, int length1)
        {
            if (!string.IsNullOrEmpty(input))
            {
                if (length1 == 0)
                {
                    length1 = length;
                }
                if (input.Length >= length1)
                {
                    String str_left = input.Substring(0, length1);
                    String str_right = input.Remove(0, length1);
                    int banjiao = 0, other = 0;
                    char[] chars = str_left.Trim().ToCharArray();
                    foreach (char ch in chars)
                    {
                        if (ch >= 33 && ch <= 126)
                        {
                            banjiao++;
                        }
                        else// if(ch>=65281 && ch<=65374)
                        {
                            other++;
                        }
                    }
                    if ((other + (float)banjiao / 2) > length || (other + (float)banjiao / 2) - length == 0.5)
                    {
                        str_left = str_left.Remove(str_left.Length - 1, 1);
                        return str_left;
                    }
                    else if ((other + (float)banjiao / 2) - length != 0)
                    {
                        if (length1 + 1 <= input.Length)
                        {
                            str_left = input.ToLimitLength(length, length1 + 1);
                        }
                    }
                    return str_left;
                }
                else
                {
                    return input;
                }
            }
            else
            {
                return input;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="emptyString"></param>
        /// <returns></returns>
        public static string ToEmpty(this object input, string emptyString = "")
        {
            if (input == null)
            {
                return emptyString;
            }
            else
            {
                string temp = input.ToString();
                return string.IsNullOrEmpty(temp) ? emptyString : temp;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToCompress(this string input)
        {
            Regex reg = new Regex(@"\s+|\r");
            string value = reg.Replace(input, " ");
            return value.Trim();
        }
    }
}
